import { useState, useEffect } from 'react';

const useCountdown = (initialSeconds = 60) => {
    const [seconds, setSeconds] = useState(initialSeconds);
    const [isCounting, setIsCounting] = useState(false);

    useEffect(() => {
        let timer = null;

        if (isCounting && seconds > 0) {
            timer = setInterval(() => {
                setSeconds(prev => prev - 1);
            }, 1000);
        } else if (seconds === 0) {
            setIsCounting(false);
        }

        return () => clearInterval(timer);
    }, [isCounting, seconds]);

    const startCountdown = () => {
        setSeconds(initialSeconds);
        setIsCounting(true);
    };

    const resetCountdown = () => {
        setSeconds(initialSeconds);
        setIsCounting(false);
    };

    return { seconds, isCounting, startCountdown, resetCountdown };
};

export default useCountdown;
